package com.cuijq.ms.Interpreterdesign.v3;

import java.util.Map;

/**
 * @author by CuiJQ
 * @Classname GreaterExpression
 * @Description GreaterExpression Description
 * @Date 2021/5/31 15:40
 */
public class GreaterExpression implements Expression {

  private String key;
  private long value;

  public GreaterExpression(String strExpression) {
    String[] elements = strExpression.trim().split("\\s+");
    if (elements.length != 3 || !elements[1].trim().equals(">")) {
      throw new RuntimeException("Expression is invalid: " + strExpression);
    }
    this.key = elements[0].trim();
    this.value = Long.parseLong(elements[2].trim());
  }

  public GreaterExpression(String key, long value) {
    this.key = key;
    this.value = value;
  }

  @Override
  public boolean interpret(Map<String, Long> stats) {
    if (!stats.containsKey(key)) {
      return false;
    }
    long statValue = stats.get(key);
    return statValue > value;
  }

}
